<%@page import="java.util.ArrayList"
import="manager.Manager"
import="bean.Medecin"
import="data.Rmi"
%>

<jsp:useBean id="manager" scope="session" class="manager.Manager"></jsp:useBean>

<% 
	if(manager.isConnected()){
		response.sendRedirect("accueil.jsp");
		return;
	}
	
	boolean err = false;
	String login = "";
	String errLogin = "";
	String password = "";
	String errPassword = "";
	if (request.getParameter("connecter") != null) {

		/* Saisie du login */
		login = request.getParameter("login");
		if (login == null)
			login = "";
		if (login.trim().equals("")) {
			err = true;
			errLogin = "Veuillez remplir le champ login.";
		}

		/* Saisie du login */
		password = request.getParameter("pwd");
		if (password == null)
			password = "";
		if (password.trim().equals("")) {
			err = true;
			errPassword = "Veuillez remplir le champ password.";
		}

		if (!err) {
			Medecin medecin = new Medecin();
			medecin.setLogin(login);
			medecin.setPassword(password);
			medecin = Rmi.authentification(medecin);
			if(medecin!=null){
				manager.setMedecin(medecin);
				manager.setConnected();
			}else{
				manager.getMessages().add("Erreur d'authentification");
				manager.getMessages().add("Veuillez re-essayer");
			}
			response.sendRedirect("index.jsp");
			return;
		}

	}
%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link rel="stylesheet" type="text/css" href="../css/index.css"/>
	<title>Authentification</title>
</head>
<body>
	<div id="page">
	
		<%
		ArrayList<String> messages = manager.extraireMessages();
		if(messages.size()>0){
			out.println("<div class=\"notif-erreur\">");
			out.println("<ul>");
			for (int i=0;i < messages.size();i++){
				out.println("<li>" + messages.get(i) + "</li>");
			}
			out.println("</ul>");
			out.println("</div>");
		}
		%>
	
		<form action="index.jsp" method="post">
		<table>
			<tr>
				<td>Identifiant:</td>
				<td><input type="text" name="login" value="" /></td>
				<td class="notif-erreur"><%=errLogin%></td>
			</tr>
		
			<tr>
				<td>Mot de passe:</td>
				<td><input type="password" name="pwd" value="" /></td>
				<td class="notif-erreur"><%=errPassword%></td>
		
			</tr>
			<tr>
				<td colspan="2" id="tdInput"><input type="submit" name="connecter" value="Se connecter" /></td>
			</tr>
		</table>
		</form>
	</div>
</body>
</html>